package cn.zspt.vank.config;

import cn.zspt.vank.exception.LoginException;
import cn.zspt.vank.exception.RegisterException;
import cn.zspt.vank.exception.TokenException;
import cn.zspt.vank.util.Result;
import cn.zspt.vank.util.ResultMsgEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class CustomerExceptionHandler {

    @ExceptionHandler(LoginException.class)
    public Result ErrorHandler(LoginException e) {
        log.error("用户名或者密码错误！", e);
        return Result.error(ResultMsgEnum.WRONG_LOGIN.getCode(),
               ResultMsgEnum.WRONG_LOGIN.getMsg());
    }

    @ExceptionHandler(TokenException.class)
    public Result TokenHandler(TokenException e) {
        log.error("令牌问题！令牌过期或者失效！", e);
        return Result.error(ResultMsgEnum.TOKEN_EXCEPTION.getCode(),
                ResultMsgEnum.TOKEN_EXCEPTION.getMsg());
    }

    @ExceptionHandler(RegisterException.class)
    public Result ErrorHandler(RegisterException e) {
        log.error("注册失败！", e);
        return Result.error(ResultMsgEnum.REGISTER_FAILED.getCode(),
                ResultMsgEnum.REGISTER_FAILED.getMsg());
    }

    @ExceptionHandler(Exception.class)
    public Result Execption(Exception e) {
        log.error("未知异常！", e);
        return Result.error(ResultMsgEnum.SERVER_BUSY.getCode(),ResultMsgEnum.SERVER_BUSY.getMsg());
    }
}